

#include "zs_timer.h"
#include <utility>
#include "msg_queue.h"
#include "tcp_conn.h"
#include "ZsLog.h"

zs::CZSTimer::CZSTimer(long interval, zs::TimerCallback callback)
    : interval_(interval), timer_(0, interval_),callback_(std::move(callback))
{
    timer_.start(Poco::TimerCallback<zs::CZSTimer>(*this, &zs::CZSTimer::timer_func));}


zs::CZSTimer::~CZSTimer() = default;

void zs::CZSTimer::timer_func(Poco::Timer &)
{
    std::string heart_beat_msg = callback_();
    LOG_INFO << "generate msg from callback: " << heart_beat_msg;
    if (zs::CTcpConn::get_instance()->is_connected())
    {
        zs::CMsgQueue::get_instance().push_msg(heart_beat_msg);
    }
    else
    {
        LOG_ERROR << "socket is not connected, to reconnect...";
        zs::CTcpConn::get_instance()->connect("8.138.135.101", 7208);
    }
}